package com.ng.desktop.hr.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.ForeignKey;

@Entity
@Table(name = "HR_TAG")
public class Tag {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID", nullable = false)
	private Long id;

	@Column(name = "NAME", nullable = false, length = 60)
	private String name;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "ID_PARENT", nullable = true)
	private Tag parent;

	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name = "ID_PARENT")
	@ForeignKey(name = "ID")
	@Cascade(value = {org.hibernate.annotations.CascadeType.ALL,
			org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private List<Tag> childs;

	@Column(name = "FULLNAME", nullable = false, length = 60)
	private String fullName;

	public String getFullName() {
		if (StringUtils.isEmpty(fullName)) {
			if (this.parent != null) {
				return this.parent.getFullName() + "\\" + this.name;
			}
			return this.name;
		} else {
			return fullName;
		}
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public Tag getParent() {
		return parent;
	}

	public List<Tag> getChilds() {
		return childs;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setParent(Tag parent) {
		this.parent = parent;
	}

	public void setChilds(List<Tag> childs) {
		this.childs = childs;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	@Override
	public String toString() {
		return this.getFullName();
	}
}
